package com.controller;

import com.domain.Book;
import com.domain.BookAndComments;
import com.domain.Comments;
import com.entity.PageResult;
import com.service.BookAndCommentsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;


import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/comments")
public class BookAndCommentsController {

    @Autowired
    private BookAndCommentsService bookAndCommentsService;


    //显示所有评论
    @RequestMapping("/searchComments")
    public String searchComments(HttpServletRequest request) {
        List<BookAndComments> list = bookAndCommentsService.selectAll();
        request.getSession().setAttribute("list",list);
        return "book_comments";

    }

     //查看书籍具体评论
     @RequestMapping("/findByIsbn")
     public ModelAndView findByIsbn(String isbn, Integer pageNum, Integer pageSize, HttpServletRequest request) {
         if (pageNum == null) {
             pageNum = 1;
         }
         if (pageSize == null) {
             pageSize = 10;
         }
         //查询到的图书信息
         PageResult pageResult = bookAndCommentsService.findByIsbn(isbn, pageNum, pageSize);
         request.getSession().setAttribute("comments",pageResult.getRows());
         ModelAndView modelAndView = new ModelAndView();
         modelAndView.setViewName("comments_detailed");
         //将查询到的数据存放在 ModelAndView的对象中
         modelAndView.addObject("pageResult", pageResult);
         System.out.println("11111111111111111111111"+pageResult.getRows());
         //将查询的参数返回到页面，用于回显到查询的输入框中
         // modelAndView.addObject("search", book);
         //将当前页码返回到页面，用于分页插件的分页显示
         modelAndView.addObject("pageNum", pageNum);
         //将当前查询的控制器路径返回到页面，页码变化时继续向该路径发送请求
         modelAndView.addObject("gourl", request.getRequestURI());
         return modelAndView;
     }

    //增加评论
    @ResponseBody
    @RequestMapping("/addComments")
    public entity.Result addComments(BookAndComments bookAndComments, Comments comments) {
        try {

            Integer count = bookAndCommentsService.addBookAndComments(bookAndComments);
            Integer count2 = bookAndCommentsService.addComments(comments);
            if (count != 1 || count2 != 1) {
                return new entity.Result(false, "新增评论失败!");
            }
            return new entity.Result(true, "新增评论成功!");
        } catch (Exception e) {
            e.printStackTrace();
            return new entity.Result(false, "新增评论失败!");
        }
    }

    //搜索评论
    @RequestMapping("/search")
    public ModelAndView search(BookAndComments bookAndComments, Integer pageNum, Integer pageSize, HttpServletRequest request) {
        if (pageNum == null) {
            pageNum = 1;
        }
        if (pageSize == null) {
            pageSize = 10;
        }
        //查询到的图书信息
        PageResult pageResult = bookAndCommentsService.search(bookAndComments, pageNum, pageSize);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("book_comments");
        //将查询到的数据存放在 ModelAndView的对象中
        modelAndView.addObject("list", pageResult.getRows());
        //将查询的参数返回到页面，用于回显到查询的输入框中
        modelAndView.addObject("search", bookAndComments);
        //将当前页码返回到页面，用于分页插件的分页显示
        modelAndView.addObject("pageNum", pageNum);
        //将当前查询的控制器路径返回到页面，页码变化时继续向该路径发送请求
        modelAndView.addObject("gourl", request.getRequestURI());
        return modelAndView;
    }
}
